﻿using System;

namespace _22_封装事件_委托与事件
{
    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();

            //让事件绑定方法
            t.Worked += (k, f) => Console.WriteLine($"你已调用了 {f} 次实例。");
            t.Run();
            t.Run();
            t.Run();
            t.Run();
            t.Run();
            t.Run();
            Console.Read();
        }
    }


    public class Test
    {
        DemoEventDelegate _myEvent;

        public event DemoEventDelegate Worked
        {
            add {
                if (value!=null)
                {
                    _myEvent += value;
                }
                 }
            remove { _myEvent -= value; }
        }

        private int c = 0;
        public void Run()
        {
            _myEvent?.Invoke(this,++c);
        }
    }

    public delegate void DemoEventDelegate(object obj, int count);

}
